<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Sports extends Model
{
    /**
     *国内新闻数据表命名
     * @var string
     */
    protected $table='sports';
    /**
     *国内新闻模型定义
     * @var
     */
    protected $countryOSportsModel;
    /**
     *模型属性定义
     * @var
     */
    public $id = 'id';
    public $title = 'title';
    public $subtitle = 'subtitle';
    public $newcat_id = 'newcat_id';
    public $key_seo = 'key_seo';
    public $key_description = 'key_description';
    public $content = 'content';
    public $title_attr = 'title_attr';
    public $title_color = 'title_color';
    public $author = 'author';
    public $source = 'source';
    public $is_check='is_check';
    public $title_url='title_url';
    public $is_hot='is_hot';
    public $is_first='is_first';
    public $images='images';
    public $status = 'status';
    public $is_delete='is_delete';
    public $created_at = 'created_at';
    public $updated_at = 'updated_at';

    /**
     * 模型数据初始化
     * CountryIn constructor.
     */
    public function __construct()
    {
        $this->countryOSportsModel = DB::table($this->table);
    }
    /**
     *国内新闻列表显示
     */
    public function getList($params){
        $data=[];
        if($params['getfield']){
            $data['total'] = $this->countryOSportsModel->where($params['where'])->count();
            $data['row'] = $this->countryOSportsModel->where($params['where'])->orderByDesc($params['order'])->select($params['field'])->offset(($params['page']-1)*$params['limit'])->limit($params['limit'])->get();
        }else{
            $data['row'] = $this->countryOSportsModel->where($params['where'])->orderByDesc($params['order'])->select($params['field'])->get();
        }
        return $data;
    }

    /**
     * @param $data
     * @return bool
     */
    public function addCountrySports($data){
        $params = [
            $this->title => $data['title'],
            $this->subtitle => $data['subtitle'],
            $this->newcat_id=>$data['newcat_id'],
            $this->key_seo => $data['key_seo'],
            $this->key_description => $data['key_description'],
            $this->content => $data['content'],
            $this->title_attr => $data['title_attr'],
            $this->title_color => $data['title_color'],
            $this->author => $data['author'],
            $this->source => $data['source'],
            $this->is_check => $data['is_check'],
            $this->title_url => $data['title_url'],
            $this->is_hot => $data['is_hot'],
            $this->is_first => $data['is_first'],
            $this->images => $data['images'],
            $this->status => 1,
            $this->is_delete => 1,
            $this->created_at => date('Y-m-d H:i;s'),
            $this->updated_at => date('Y-m-d H:i;s')
        ];
        $rs = $this->countryOSportsModel->insert($params);
        return $rs;
    }

    /**
     * @param $where
     * @return Model|\Illuminate\Database\Query\Builder|null|object
     */
    public function oneFind($where){
        return $this->countryOSportsModel->where($where)->first();
    }

    /**
     * @param $data
     * @return int
     */
    public function updateCountrySports($data){
        $rs = $this->countryOSportsModel->where($data['where'])->update($data['field']);
        return $rs;
    }
}
